package com.ruoyi.app.service.Impl;

import com.ruoyi.app.service.ChargeService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.app.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.mapper.app.DeviceMapper;
import com.ruoyi.system.mapper.app.DeviceSpaceMapper;
import com.ruoyi.system.mapper.app.RechargeOptionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ChargeServiceImpl implements ChargeService {

    @Autowired
    DeviceMapper deviceMapper;
    @Autowired
    ChargeOptionMapper chargeOptionMapper;
    @Autowired
    CardChargeOptionMapper cardChargeOptionMapper;
    @Autowired
    ComboMapper comboMapper;
    @Autowired
    ConsumptionOptionMapper consumptionOptionMapper;
    @Autowired
    PowerBillingMapper powerBillingMapper;
    @Autowired
    DeviceSpaceMapper deviceSpaceMapper;
    @Autowired
    CommunityMapper communityMapper;
    @Autowired
    RechargeOptionMapper rechargeOptionMapper;


    @Override
    public List<Device> nearChargeDevice(Double longitude, Double latitude,String communityName,String appId) {
        List<Device> deviceList = deviceMapper.getAll(communityName,appId);
        List<Device> devices = new ArrayList<>();
        for (Device device : deviceList) {
            if (StringUtils.isNotNull(device.getLatitude()) && StringUtils.isNotNull(device.getLongitude())) {
                Double dlongitude = device.getLongitude();
                Double dlatitude = device.getLatitude();
                if (dlongitude < (longitude + 0.2) && dlongitude > (longitude - 0.2)) {
                    if (dlatitude < (latitude + 0.2) && dlatitude > (latitude - 0.2)) {
                        Map<String,Long> map = deviceMapper.selectHoleAmount(device.getId());
                        device.setBusyAmount(map.get("busy").intValue());
                        device.setIdleAmount(map.get("idle").intValue());
                        devices.add(device);
                    }
                }
            }
        }
        return devices;
    }

    @Override
    public List<Community> nearChargeCommunity(Double longitude, Double latitude, String communityName, String appId) {
        List<Community> communityList = communityMapper.getAll(communityName,appId);
        List<Community> communities = new ArrayList<>();
        for (Community community : communityList) {
            if (StringUtils.isNotNull(community.getLatitude()) && StringUtils.isNotNull(community.getLongitude())) {
                Double dlongitude = Double.valueOf(community.getLongitude());
                Double dlatitude = Double.valueOf(community.getLatitude());
                if (dlongitude < (longitude + 0.2) && dlongitude > (longitude - 0.2)) {
                    if (dlatitude < (latitude + 0.2) && dlatitude > (latitude - 0.2)) {
//                        Map<String,Long> map = deviceMapper.selectHoleAmount(device.getId());
//                        device.setBusyAmount(map.get("busy").intValue());
//                        device.setIdleAmount(map.get("idle").intValue());
                        communities.add(community);
                    }
                }
            }
        }
        return communities;
    }

    @Override
    public Map<String, Object> getQcDeviceInfo(String qcCode) {
        DeviceSpace deviceSpace = deviceSpaceMapper.getByBindNo(qcCode);
        if (deviceSpace == null) {
            throw new ServiceException("二维码未绑定插座");
        }
        Device device = deviceMapper.selectByPrimaryKey(deviceSpace.getDeviceId());
        Map<String, Object> map = new HashMap<>();
        map.put("socket",deviceSpace);
        map.put("device",device);
        return map;
    }

    @Override
    public List<Device> getDeviceListByCommunityId(Long communityId) {
        Device device = new Device();
        device.setCommunityId(communityId);
        List<Device> deviceList = deviceMapper.deviceList(device);
        if (!deviceList.isEmpty()) {
            List<Map> maps = deviceSpaceMapper.getSocketStateList(deviceList);
            for (Device device1 : deviceList) {
                for (Map map : maps) {
                    if (device1.getId().equals(map.get("deviceId"))) {
                        if ((int)map.get("state") == 0) {
                            device1.setIdleAmount(Long.valueOf(map.get("stateCount")+"").intValue());
                        }
                        if ((int)map.get("state")==1) {
                            device1.setBusyAmount(Long.valueOf(map.get("stateCount")+"").intValue());
                        }
                        if ((int)map.get("state")==2) {
                            device1.setFaultAmount( Long.valueOf(map.get("stateCount")+"").intValue());
                        }
                    }
                }
            }
        }
        return deviceList;
    }

    @Override
    public List<DeviceSpace> sockets(Long deviceId) {
        return deviceSpaceMapper.getByDeviceId(deviceId);
    }

    @Override
    public List<Device> findDevice(String value,String appId) {
        return deviceMapper.findDevice(value,appId);
    }

    @Override
    public List<ChargeOption> balanceChargeOption(Long communityId) {
        return chargeOptionMapper.getChargeList(communityId);
    }

    @Override
    public List<RechargeOption> rechargeOption(Long communityId) {
        return rechargeOptionMapper.getList(communityId);
    }

    @Override
    public List<Combo> balanceComboOption(Long communityId) {
        return comboMapper.getComboList(communityId);
    }

    @Override
    public List<CardChargeOption> balanceCardOption(Long communityId) {
        return cardChargeOptionMapper.getCardChargeList(communityId);
    }

    @Override
    public Device getDeviceInfo(String deviceNum) {
        Device device  = deviceMapper.selectByNum(deviceNum);
        if (StringUtils.isNull(device)) {
            throw new ServiceException("设备不存在");
        }
        Map<String,Long> map = deviceMapper.selectHoleAmount(device.getId());
        device.setBusyAmount(map.get("busy").intValue());
        device.setIdleAmount(map.get("idle").intValue());
        return device;
    }

    @Override
    public List<ConsumptionOption> consumptionOption(Long communityId) {
        return consumptionOptionMapper.getTimeOption(communityId);
    }

    @Override
    public List<PowerBilling> powerList(Long deviceId) {
        return powerBillingMapper.getPowerBilling(deviceId);
    }
}
